﻿// -----------------------------------------------------------------------
// <copyright file="CollisionDetection.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SpaceshipDuel
{
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;

    /// <summary>
    /// Führt die Kollisionserkennung durch
    /// </summary>
    public static class CollisionDetection
    {
        #region Public methods

        /// <summary>
        /// Kollisionserkennung für Schüsse
        /// Wird für alle Schüsse verwendet (Spieler1, Spieler2 und Gegner)
        /// </summary>
        public static void Bullets(PlayerShip ship1, PlayerShip ship2, List<Bullet> bullets, List<Asteroid> asteroids, List<Mine> mines, List<Turret> turrets, 
            List<AlienShip1> alienShips1, List<AlienShip2> alienShips2, List<AlienShip3> alienShips3, List<AlienShip4> alienShips4, int worldSize)
        {
            for (int i = 0; i < bullets.Count; i++)
            {
                if (ship1.CurrentShipState == ShipState.Alive)
                {
                    if (CollisionHelper.Collision(bullets[i].Center, 4, ship1.Center, 32, worldSize))
                    {
                        bullets[i].DestroyMe = true;
                        ship1.Hit(bullets[i].Damage);
                        SoundProviderOgg.SfxPlayershipHit.Play();
                        continue;
                    }
                }
                if (ship2.CurrentShipState == ShipState.Alive)
                {
                    if (CollisionHelper.Collision(bullets[i].Center, 4, ship2.Center, 32, worldSize))
                    {
                        bullets[i].DestroyMe = true;
                        ship2.Hit(bullets[i].Damage);
                        SoundProviderOgg.SfxPlayershipHit.Play();
                        continue;
                    }
                }

                for (int j = 0; j < asteroids.Count; j++)
                {
                    if (CollisionHelper.Collision(bullets[i].Center, 4, asteroids[j].Center, asteroids[j].Radius, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(TextureProvider.ColBullet, bullets[i].Center, bullets[i].RotationAngle, GetAsteroidColorArray(asteroids[j].Size), asteroids[j].Center, asteroids[j].RotationAngle))
                        {
                            asteroids[j].Hit(bullets[i].Damage);
                            bullets[i].DestroyMe = true;
                            SoundProviderOgg.SfxObjectHit.Play();
                            goto NextBullet;
                        }
                    }
                }

                for (int j = 0; j < mines.Count; j++)
                {
                    if (CollisionHelper.Collision(bullets[i].Center, 4, mines[j].Center, mines[j].Radius, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(TextureProvider.ColBullet, bullets[i].Center, bullets[i].RotationAngle, TextureProvider.ColMine, mines[j].Center, mines[j].RotationAngle))
                        {
                            mines[j].Hit(bullets[i].Damage);
                            bullets[i].DestroyMe = true;
                            SoundProviderOgg.SfxObjectHit.Play();
                            goto NextBullet;
                        }
                    }
                }

                // turrets without pixel-perfect collision (they are circles)
                for (int j = 0; j < turrets.Count; j++)
                {
                    if (CollisionHelper.Collision(bullets[i].Center, 4, turrets[j].Center, turrets[j].Radius, worldSize))
                    {
                        turrets[j].Hit(bullets[i].Damage);
                        bullets[i].DestroyMe = true;
                        SoundProviderOgg.SfxObjectHit.Play();
                        goto NextBullet;
                    }
                }

                for (int j = 0; j < alienShips1.Count; j++)
                {
                    if (CollisionHelper.Collision(bullets[i].Center, 4, alienShips1[j].Center, alienShips1[j].Radius, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(TextureProvider.ColBullet, bullets[i].Center, bullets[i].RotationAngle, TextureProvider.ColAlien1, alienShips1[j].Center, alienShips1[j].RotationAngle))
                        {
                            alienShips1[j].Hit(bullets[i].Damage);
                            bullets[i].DestroyMe = true;
                            SoundProviderOgg.SfxObjectHit.Play();
                            goto NextBullet;
                        }
                    }
                }

                for (int j = 0; j < alienShips2.Count; j++)
                {
                    if (CollisionHelper.Collision(bullets[i].Center, 4, alienShips2[j].Center, alienShips2[j].Radius, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(TextureProvider.ColBullet, bullets[i].Center, bullets[i].RotationAngle, TextureProvider.ColAlien2, alienShips2[j].Center, alienShips2[j].RotationAngle))
                        {
                            alienShips2[j].Hit(bullets[i].Damage);
                            bullets[i].DestroyMe = true;
                            SoundProviderOgg.SfxObjectHit.Play();
                            goto NextBullet;
                        }
                    }
                }

                for (int j = 0; j < alienShips3.Count; j++)
                {
                    if (CollisionHelper.Collision(bullets[i].Center, 4, alienShips3[j].Center, alienShips3[j].Radius, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(TextureProvider.ColBullet, bullets[i].Center, bullets[i].RotationAngle, TextureProvider.ColAlien3, alienShips3[j].Center, alienShips3[j].RotationAngle))
                        {
                            alienShips3[j].Hit(bullets[i].Damage);
                            bullets[i].DestroyMe = true;
                            SoundProviderOgg.SfxObjectHit.Play();
                            goto NextBullet;
                        }
                    }
                }

                for (int j = 0; j < alienShips4.Count; j++)
                {
                    if (CollisionHelper.Collision(bullets[i].Center, 4, alienShips4[j].Center, alienShips4[j].Radius, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(TextureProvider.ColBullet, bullets[i].Center, bullets[i].RotationAngle, TextureProvider.ColAlien4, alienShips4[j].Center, alienShips4[j].RotationAngle))
                        {
                            alienShips4[j].Hit(bullets[i].Damage);
                            bullets[i].DestroyMe = true;
                            SoundProviderOgg.SfxObjectHit.Play();
                            goto NextBullet;
                        }
                    }
                }

                // Sprungmarke - falls ein Schuss etwas getroffen hat, müssen keine 
                // weiteren Kollisionsabfragen für den Schuss gemacht werden
                NextBullet:
                    continue;
            }
        }

        
        /// <summary>
        /// Kollisionen vom Spieler-Raumschiff mit Objekten
        /// </summary>
        public static void PlayerShip(PlayerShip ship, Color[,] shipColor, List<Upgrade> upgrades, List<Asteroid> asteroids, List<Mine> mines, List<Turret> turrets, 
            List<AlienShip1> alienShips1, List<AlienShip2> alienShips2, List<AlienShip3> alienShips3, List<AlienShip4> alienShips4, int worldSize)
        {
            if (ship.CurrentShipState == ShipState.Alive)
            {
                // Kollision mit Upgrades
                for (int i = 0; i < upgrades.Count; i++)
                {
                    if (CollisionHelper.Collision(upgrades[i].Center, 16, ship.Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, ship.Center, ship.RotationAngle, TextureProvider.ColUpgrades, upgrades[i].Center, 0) == true)
                        {
                            ship.Upgrade(upgrades[i].Type);
                            upgrades[i].DestroyMe = true;
                            SoundProviderOgg.SfxUpgradeCollected.Play();
                        }
                    }
                }

                // Kollision mit Asteroiden
                for (int i = 0; i < asteroids.Count; i++)
                {
                    if (CollisionHelper.Collision(asteroids[i].Center, asteroids[i].Radius, ship.Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, ship.Center, ship.RotationAngle, GetAsteroidColorArray(asteroids[i].Size), asteroids[i].Center, asteroids[i].RotationAngle) == true)
                        {
                            switch (asteroids[i].Size)
                            {
                                case AsteroidSize.small: ship.Hit(10); break;
                                case AsteroidSize.medium: ship.Hit(50); break;
                                case AsteroidSize.big: ship.Hit(100); break;
                            }
                            asteroids[i].Hit(100);
                        }
                    }
                }

                // Kollision mit Mine
                for (int i = 0; i < mines.Count; i++)
                {
                    // Annäherung löst Detonationscountdown aus
                    if (CollisionHelper.Collision(mines[i].Center, 300, ship.Center, 32, worldSize))
                    {
                        if (mines[i].StartDetonation == false)
                            mines[i].StartDetonation = true;
                    }

                    if (CollisionHelper.Collision(mines[i].Center, 16, ship.Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, ship.Center, ship.RotationAngle, TextureProvider.ColMine, mines[i].Center, 0) == true)
                        {
                            ship.Hit(100);
                            mines[i].DestroyMe = true;
                        }
                    }
                }

                // Kollision mit Turm
                for (int i = 0; i < turrets.Count; i++)
                {
                    if (CollisionHelper.Collision(turrets[i].Center, turrets[i].Radius, ship.Center, 32, worldSize))
                    {
                        ship.Hit(100);
                        turrets[i].DestroyMe = true;
                    }
                }

                // Kollision mit alienShips1
                for (int i = 0; i < alienShips1.Count; i++)
                {
                    if (CollisionHelper.Collision(alienShips1[i].Center, 16, ship.Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, ship.Center, ship.RotationAngle, TextureProvider.ColAlien1, alienShips1[i].Center, alienShips1[i].RotationAngle) == true)
                        {
                            ship.Hit(60);
                            alienShips1[i].Hit(300);
                        }
                    }
                }

                // Kollision mit alienShips2
                for (int i = 0; i < alienShips2.Count; i++)
                {
                    if (ship.PlayerNumber == 1)                 // Position des Spielers an Alienschiffklasse
                        alienShips2[i].Player1 = ship.Center;   // übergeben, damit diese zielen kann ;-)
                    else
                        alienShips2[i].Player2 = ship.Center;

                    if (CollisionHelper.Collision(alienShips2[i].Center, 16, ship.Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, ship.Center, ship.RotationAngle, TextureProvider.ColAlien2, alienShips2[i].Center, alienShips2[i].RotationAngle) == true)
                        {
                            ship.Hit(60);
                            alienShips2[i].Hit(300);
                        }
                    }
                }

                // Kollision mit alienShips3
                for (int i = 0; i < alienShips3.Count; i++)
                {
                    if (ship.PlayerNumber == 1)
                        alienShips3[i].Player1 = ship.Center;
                    else
                        alienShips3[i].Player2 = ship.Center;

                    if (CollisionHelper.Collision(alienShips3[i].Center, 16, ship.Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, ship.Center, ship.RotationAngle, TextureProvider.ColAlien3, alienShips3[i].Center, alienShips3[i].RotationAngle) == true)
                        {
                            ship.Hit(60);
                            alienShips3[i].Hit(300);
                        }
                    }
                }

                // Kollision mit alienShips4
                for (int i = 0; i < alienShips4.Count; i++)
                {
                    if (ship.PlayerNumber == 1)
                        alienShips4[i].Player1 = ship.Center;
                    else
                        alienShips4[i].Player2 = ship.Center;

                    if (CollisionHelper.Collision(alienShips4[i].Center, 16, ship.Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, ship.Center, ship.RotationAngle, TextureProvider.ColAlien4, alienShips4[i].Center, alienShips4[i].RotationAngle) == true)
                        {
                            ship.Hit(100);
                            alienShips4[i].Hit(300);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Kollision von Alien-Raumschiffen Typ 1
        /// </summary>
        public static void AlienShips1(List<Asteroid> asteroids, List<Mine> mines, List<Turret> turrets, List<AlienShip1> alienShips1, List<AlienShip2> alienShips2, List<AlienShip3> alienShips3, List<AlienShip4> alienShips4, int worldSize)
        {
            Color[,] shipColor = TextureProvider.ColAlien1;

            float minDistance; 
            float currentDistance;

            for (int k = 0; k < alienShips1.Count; k++)
            {
                minDistance = 10000;

                // Kollision mit Asteroiden
                for (int i = 0; i < asteroids.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips1[k].Center, asteroids[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips1[k].NearestObstacle = asteroids[i].Center;
                    }

                    if (CollisionHelper.Collision(asteroids[i].Center, asteroids[i].Radius, alienShips1[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips1[k].Center, alienShips1[k].RotationAngle, GetAsteroidColorArray(asteroids[i].Size), asteroids[i].Center, asteroids[i].RotationAngle) == true)
                        {
                            switch (asteroids[i].Size)
                            {
                                case AsteroidSize.small: alienShips1[k].Hit(10); break;
                                case AsteroidSize.medium: alienShips1[k].Hit(50); break;
                                case AsteroidSize.big: alienShips1[k].Hit(100); break;
                            }
                            asteroids[i].Hit(100);
                        }
                    }
                }

                // Kollision mit Mine
                for (int i = 0; i < mines.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips1[k].Center, mines[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips1[k].NearestObstacle = mines[i].Center;
                    }

                    if (CollisionHelper.Collision(mines[i].Center, 16, alienShips1[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips1[k].Center, alienShips1[k].RotationAngle, TextureProvider.ColMine, mines[i].Center, 0) == true)
                        {
                            alienShips1[k].Hit(100);
                            mines[i].DestroyMe = true;
                        }
                    }
                }

                // Kollision mit Turm
                for (int i = 0; i < turrets.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips1[k].Center, turrets[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips1[k].NearestObstacle = turrets[i].Center;
                    }

                    if (CollisionHelper.Collision(turrets[i].Center, turrets[i].Radius, alienShips1[k].Center, 32, worldSize))
                    {
                        alienShips1[k].Hit(100);
                        turrets[i].DestroyMe = true;
                    }
                }

                // Kollision mit anderen Alienschiffen vom gleichen Typ
                for (int i = 0; i < alienShips1.Count; i++)
                {
                    if (i != k)
                    {
                        currentDistance = CollisionHelper.GetDistance(alienShips1[k].Center, alienShips1[i].Center, worldSize);
                        if (currentDistance < minDistance)
                        {
                            minDistance = currentDistance;
                            alienShips1[k].NearestObstacle = alienShips1[i].Center;
                        }

                        if (CollisionHelper.Collision(alienShips1[i].Center, 32, alienShips1[k].Center, 32, worldSize))
                        {
                            if (CollisionHelper.PixelCollision(shipColor, alienShips1[k].Center, alienShips1[k].RotationAngle, TextureProvider.ColAlien1, alienShips1[i].Center, alienShips1[i].RotationAngle) == true)
                            {
                                alienShips1[k].Hit(100);
                                alienShips1[i].Hit(100);
                            }
                        }
                    }
                }

                for (int i = 0; i < alienShips2.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips1[k].Center, alienShips2[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips1[k].NearestObstacle = alienShips2[i].Center;
                    }

                    if (CollisionHelper.Collision(alienShips2[i].Center, 32, alienShips1[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips1[k].Center, alienShips1[k].RotationAngle, TextureProvider.ColAlien2, alienShips2[i].Center, alienShips2[i].RotationAngle) == true)
                        {
                            alienShips1[k].Hit(100);
                            alienShips2[i].Hit(100);
                        }
                    }
                }

                for (int i = 0; i < alienShips3.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips1[k].Center, alienShips3[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips1[k].NearestObstacle = alienShips3[i].Center;
                    }

                    if (CollisionHelper.Collision(alienShips3[i].Center, 32, alienShips1[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips1[k].Center, alienShips1[k].RotationAngle, TextureProvider.ColAlien3, alienShips3[i].Center, alienShips3[i].RotationAngle) == true)
                        {
                            alienShips1[k].Hit(100);
                            alienShips3[i].Hit(100);
                        }
                    }
                }

                for (int i = 0; i < alienShips4.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips1[k].Center, alienShips4[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips1[k].NearestObstacle = alienShips4[i].Center;
                    }

                    if (CollisionHelper.Collision(alienShips4[i].Center, 32, alienShips1[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips1[k].Center, alienShips1[k].RotationAngle, TextureProvider.ColAlien4, alienShips4[i].Center, alienShips4[i].RotationAngle) == true)
                        {
                            alienShips1[k].Hit(100);
                            alienShips4[i].Hit(100);
                        }
                    }
                }
                
            }
        }

        /// <summary>
        /// Kollision von Alien-Raumschiffen Typ 2
        /// </summary>
        public static void AlienShips2(List<Asteroid> asteroids, List<Mine> mines, List<Turret> turrets, List<AlienShip1> alienShips1, List<AlienShip2> alienShips2, List<AlienShip3> alienShips3, List<AlienShip4> alienShips4, int worldSize)
        {
            Color[,] shipColor = TextureProvider.ColAlien2;

            float minDistance;
            float currentDistance;

            for (int k = 0; k < alienShips2.Count; k++)
            {
                minDistance = 10000;

                // Kollision mit Asteroiden
                for (int i = 0; i < asteroids.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips2[k].Center, asteroids[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips2[k].NearestObstacle = asteroids[i].Center;
                    }

                    if (CollisionHelper.Collision(asteroids[i].Center, asteroids[i].Radius, alienShips2[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips2[k].Center, alienShips2[k].RotationAngle, GetAsteroidColorArray(asteroids[i].Size), asteroids[i].Center, asteroids[i].RotationAngle) == true)
                        {
                            switch (asteroids[i].Size)
                            {
                                case AsteroidSize.small: alienShips2[k].Hit(10); break;
                                case AsteroidSize.medium: alienShips2[k].Hit(50); break;
                                case AsteroidSize.big: alienShips2[k].Hit(100); break;
                            }
                            asteroids[i].Hit(100);
                        }
                    }
                }

                // Kollision mit Mine
                for (int i = 0; i < mines.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips2[k].Center, mines[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips2[k].NearestObstacle = mines[i].Center;
                    }

                    if (CollisionHelper.Collision(mines[i].Center, 16, alienShips2[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips2[k].Center, alienShips2[k].RotationAngle, TextureProvider.ColMine, mines[i].Center, 0) == true)
                        {
                            alienShips2[k].Hit(100);
                            mines[i].DestroyMe = true;
                        }
                    }
                }

                // Kollision mit Turm
                for (int i = 0; i < turrets.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips2[k].Center, turrets[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips2[k].NearestObstacle = turrets[i].Center;
                    }

                    if (CollisionHelper.Collision(turrets[i].Center, turrets[i].Radius, alienShips2[k].Center, 32, worldSize))
                    {
                        alienShips2[k].Hit(100);
                        turrets[i].DestroyMe = true;
                    }
                }

                // Kollision mit anderen Alienschiffen vom gleichen Typ
                for (int i = 0; i < alienShips2.Count; i++)
                {
                    if (i != k)
                    {
                        currentDistance = CollisionHelper.GetDistance(alienShips2[k].Center, alienShips2[i].Center, worldSize);
                        if (currentDistance < minDistance)
                        {
                            minDistance = currentDistance;
                            alienShips2[k].NearestObstacle = alienShips2[i].Center;
                        }

                        if (CollisionHelper.Collision(alienShips2[i].Center, 32, alienShips2[k].Center, 32, worldSize))
                        {
                            if (CollisionHelper.PixelCollision(shipColor, alienShips2[k].Center, alienShips2[k].RotationAngle, TextureProvider.ColAlien2, alienShips2[i].Center, alienShips2[i].RotationAngle) == true)
                            {
                                alienShips2[k].Hit(100);
                                alienShips2[i].Hit(100);
                            }
                        }
                    }
                }

                for (int i = 0; i < alienShips3.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips2[k].Center, alienShips3[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips2[k].NearestObstacle = alienShips3[i].Center;
                    }

                    if (CollisionHelper.Collision(alienShips3[i].Center, 32, alienShips2[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips2[k].Center, alienShips2[k].RotationAngle, TextureProvider.ColAlien3, alienShips3[i].Center, alienShips3[i].RotationAngle) == true)
                        {
                            alienShips2[k].Hit(100);
                            alienShips3[i].Hit(100);
                        }
                    }
                }

                for (int i = 0; i < alienShips4.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips2[k].Center, alienShips4[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips2[k].NearestObstacle = alienShips4[i].Center;
                    }

                    if (CollisionHelper.Collision(alienShips4[i].Center, 32, alienShips2[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips2[k].Center, alienShips2[k].RotationAngle, TextureProvider.ColAlien4, alienShips4[i].Center, alienShips4[i].RotationAngle) == true)
                        {
                            alienShips2[k].Hit(100);
                            alienShips4[i].Hit(100);
                        }
                    }
                }

            }
        }

        /// <summary>
        /// Kollision von Alien-Raumschiffen Typ 3
        /// </summary>
        public static void AlienShips3(List<Asteroid> asteroids, List<Mine> mines, List<Turret> turrets, List<AlienShip1> alienShips1, List<AlienShip2> alienShips2, List<AlienShip3> alienShips3, List<AlienShip4> alienShips4, int worldSize)
        {
            Color[,] shipColor = TextureProvider.ColAlien3;

            float minDistance;
            float currentDistance;

            for (int k = 0; k < alienShips3.Count; k++)
            {
                minDistance = 10000;

                // Kollision mit Asteroiden
                for (int i = 0; i < asteroids.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips3[k].Center, asteroids[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips3[k].NearestObstacle = asteroids[i].Center;
                    }

                    if (CollisionHelper.Collision(asteroids[i].Center, asteroids[i].Radius, alienShips3[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips3[k].Center, alienShips3[k].RotationAngle, GetAsteroidColorArray(asteroids[i].Size), asteroids[i].Center, asteroids[i].RotationAngle) == true)
                        {
                            switch (asteroids[i].Size)
                            {
                                case AsteroidSize.small: alienShips3[k].Hit(10); break;
                                case AsteroidSize.medium: alienShips3[k].Hit(50); break;
                                case AsteroidSize.big: alienShips3[k].Hit(100); break;
                            }
                            asteroids[i].Hit(100);
                        }
                    }
                }

                // Kollision mit Mine
                for (int i = 0; i < mines.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips3[k].Center, mines[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips3[k].NearestObstacle = mines[i].Center;
                    }

                    if (CollisionHelper.Collision(mines[i].Center, 16, alienShips3[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips3[k].Center, alienShips3[k].RotationAngle, TextureProvider.ColMine, mines[i].Center, 0) == true)
                        {
                            alienShips3[k].Hit(100);
                            mines[i].DestroyMe = true;
                        }
                    }
                }

                // Kollision mit Turm
                for (int i = 0; i < turrets.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips3[k].Center, turrets[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips3[k].NearestObstacle = turrets[i].Center;
                    }

                    if (CollisionHelper.Collision(turrets[i].Center, turrets[i].Radius, alienShips3[k].Center, 32, worldSize))
                    {
                        alienShips3[k].Hit(100);
                        turrets[i].DestroyMe = true;
                    }
                }

                // Kollision mit anderen Alienschiffen vom gleichen Typ
                for (int i = 0; i < alienShips3.Count; i++)
                {
                    if (i != k)
                    {
                        currentDistance = CollisionHelper.GetDistance(alienShips3[k].Center, alienShips3[i].Center, worldSize);
                        if (currentDistance < minDistance)
                        {
                            minDistance = currentDistance;
                            alienShips3[k].NearestObstacle = alienShips3[i].Center;
                        }

                        if (CollisionHelper.Collision(alienShips3[i].Center, 32, alienShips3[k].Center, 32, worldSize))
                        {
                            if (CollisionHelper.PixelCollision(shipColor, alienShips3[k].Center, alienShips3[k].RotationAngle, TextureProvider.ColAlien3, alienShips3[i].Center, alienShips3[i].RotationAngle) == true)
                            {
                                alienShips3[k].Hit(100);
                                alienShips3[i].Hit(100);
                            }
                        }
                    }
                }

                for (int i = 0; i < alienShips4.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips3[k].Center, alienShips4[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips3[k].NearestObstacle = alienShips4[i].Center;
                    }

                    if (CollisionHelper.Collision(alienShips4[i].Center, 32, alienShips3[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips3[k].Center, alienShips3[k].RotationAngle, TextureProvider.ColAlien4, alienShips4[i].Center, alienShips4[i].RotationAngle) == true)
                        {
                            alienShips3[k].Hit(100);
                            alienShips4[i].Hit(100);
                        }
                    }
                }

            }
        }

        /// <summary>
        /// Kollision von Alien-Raumschiffen Typ 4
        /// </summary>
        public static void AlienShips4(List<Asteroid> asteroids, List<Mine> mines, List<Turret> turrets, List<AlienShip1> alienShips1, List<AlienShip2> alienShips2, List<AlienShip3> alienShips3, List<AlienShip4> alienShips4, int worldSize)
        {
            Color[,] shipColor = TextureProvider.ColAlien4;

            float minDistance;
            float currentDistance;

            for (int k = 0; k < alienShips4.Count; k++)
            {
                minDistance = 10000;

                // Kollision mit Asteroiden
                for (int i = 0; i < asteroids.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips4[k].Center, asteroids[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips4[k].NearestObstacle = asteroids[i].Center;
                    }

                    if (CollisionHelper.Collision(asteroids[i].Center, asteroids[i].Radius, alienShips4[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips4[k].Center, alienShips4[k].RotationAngle, GetAsteroidColorArray(asteroids[i].Size), asteroids[i].Center, asteroids[i].RotationAngle) == true)
                        {
                            switch (asteroids[i].Size)
                            {
                                case AsteroidSize.small: alienShips4[k].Hit(10); break;
                                case AsteroidSize.medium: alienShips4[k].Hit(50); break;
                                case AsteroidSize.big: alienShips4[k].Hit(100); break;
                            }
                            asteroids[i].Hit(100);
                        }
                    }
                }

                // Kollision mit Mine
                for (int i = 0; i < mines.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips4[k].Center, mines[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips4[k].NearestObstacle = mines[i].Center;
                    }

                    if (CollisionHelper.Collision(mines[i].Center, 16, alienShips4[k].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(shipColor, alienShips4[k].Center, alienShips4[k].RotationAngle, TextureProvider.ColMine, mines[i].Center, 0) == true)
                        {
                            alienShips4[k].Hit(100);
                            mines[i].DestroyMe = true;
                        }
                    }
                }

                // Kollision mit Turm
                for (int i = 0; i < turrets.Count; i++)
                {
                    currentDistance = CollisionHelper.GetDistance(alienShips4[k].Center, turrets[i].Center, worldSize);
                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        alienShips4[k].NearestObstacle = turrets[i].Center;
                    }

                    if (CollisionHelper.Collision(turrets[i].Center, turrets[i].Radius, alienShips4[k].Center, 32, worldSize))
                    {
                        alienShips4[k].Hit(100);
                        turrets[i].DestroyMe = true;
                    }
                }

                // Kollision mit anderen Alienschiffen vom gleichen Typ
                for (int i = 0; i < alienShips4.Count; i++)
                {
                    if (i != k)
                    {
                        currentDistance = CollisionHelper.GetDistance(alienShips4[k].Center, alienShips4[i].Center, worldSize);
                        if (currentDistance < minDistance)
                        {
                            minDistance = currentDistance;
                            alienShips4[k].NearestObstacle = alienShips4[i].Center;
                        }

                        if (CollisionHelper.Collision(alienShips4[i].Center, 32, alienShips4[k].Center, 32, worldSize))
                        {
                            if (CollisionHelper.PixelCollision(shipColor, alienShips4[k].Center, alienShips4[k].RotationAngle, TextureProvider.ColAlien4, alienShips4[i].Center, alienShips4[i].RotationAngle) == true)
                            {
                                alienShips4[k].Hit(100);
                                alienShips4[i].Hit(100);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Kollisionserkennung der beiden Spielerschiffe
        /// </summary>
        public static void PlayerShips(PlayerShip ship1, PlayerShip ship2, int worldSize)
        {
            if (ship1.CurrentShipState == ShipState.Alive && ship2.CurrentShipState == ShipState.Alive)
            {
                if (CollisionHelper.Collision(ship1.Center, 30, ship2.Center, 30, worldSize))
                {
                    ship1.Hit();
                    ship2.Hit();
                    Vector2 resultVelocityShip1 = new Vector2();
                    Vector2 resultVelocityShip2 = new Vector2();
                    CollisionHelper.ElasticCollision(ship1.Center, ship2.Center, ship1.Velocity, ship2.Velocity, worldSize, ref resultVelocityShip1, ref resultVelocityShip2);
                    ship1.Velocity = resultVelocityShip1;
                    ship2.Velocity = resultVelocityShip2;
                }
            }
        }


        /// <summary>
        /// Annäherung eines Objektes an einen Turm löst Öffnen aus, 
        /// wenn sich kein Objekt in der Nähe befindet, dann schließt sich
        /// der Turm wieder
        /// </summary>
        public static void ProximityDetectionTurrets(PlayerShip ship1, PlayerShip ship2, List<Turret> turrets, int worldSize)
        {
            int proximityDistance = 380;
            bool proximityAlert = false;
            Vector2 target = new Vector2(0, 0);

            for (int i = 0; i < turrets.Count; i++)
            {
                if (turrets[i].State == TurretState.closing || turrets[i].State == TurretState.opening)
                    continue;

                proximityAlert = ProximityDetectionTurret(ship1, ship2, turrets[i].Center, proximityDistance, ref target, worldSize);

                // Annäherung löst öffnen und schießen aus
                if (proximityAlert == true)
                {
                    if (turrets[i].State == TurretState.closed)
                        turrets[i].State = TurretState.opening;
                    else if (turrets[i].State == TurretState.open)
                        turrets[i].SetTarget(target);
                }
                // Entfernen löst schließen aus
                else if (turrets[i].State == TurretState.open && proximityAlert == false)
                {
                    turrets[i].State = TurretState.closing;
                }

            }
        }

        /// <summary>
        /// Kollisionserkennung Atomraketen
        /// </summary>
        /// <param name="atomRockets"></param>
        /// <param name="asteroids"></param>
        /// <param name="mines"></param>
        /// <param name="turrets"></param>
        /// <param name="alienShips1"></param>
        /// <param name="alienShips2"></param>
        /// <param name="alienShips3"></param>
        /// <param name="alienShips4"></param>
        /// <param name="ship1"></param>
        /// <param name="ship2"></param>
        /// <param name="bullets"></param>
        /// <param name="worldSize"></param>
        public static void Atomrockets(List<AtomRocket> atomRockets, List<Asteroid> asteroids, List<Mine> mines, List<Turret> turrets, List<AlienShip1> alienShips1, List<AlienShip2> alienShips2,
            List<AlienShip3> alienShips3, List<AlienShip4> alienShips4, PlayerShip ship1, PlayerShip ship2, int worldSize)
        {
            int j;
            Color[,] rocketColor = TextureProvider.ColAtomRocket;

            for (int i = 0; i < atomRockets.Count; i++)
            {

                for (j = 0; j < atomRockets.Count; j++)
                {
                    if ((i != j) && CollisionHelper.Collision(atomRockets[i].Center, 23, atomRockets[j].Center, 23, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(rocketColor, atomRockets[i].Center, 0, rocketColor, atomRockets[j].Center, 0) == true)
                        {
                            atomRockets[j].DestroyMe = true;
                            atomRockets[i].DestroyMe = true;
                        }
                    }
                }

                for (j = 0; j < asteroids.Count; j++)
                {
                    if (CollisionHelper.Collision(atomRockets[i].Center, 23, asteroids[j].Center, asteroids[j].Radius, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(rocketColor, atomRockets[i].Center, 0, GetAsteroidColorArray(asteroids[j].Size), asteroids[j].Center, asteroids[j].RotationAngle) == true)
                            atomRockets[i].DestroyMe = true;
                    }
                }

                for (j = 0; j < mines.Count; j++)
                {
                    if (CollisionHelper.Collision(atomRockets[i].Center, 23, mines[j].Center, 16, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(rocketColor, atomRockets[i].Center, 0, TextureProvider.ColMine, mines[j].Center, mines[j].RotationAngle) == true)
                            atomRockets[i].DestroyMe = true;
                    }
                }

                for (j = 0; j < turrets.Count; j++)
                {
                    if (CollisionHelper.Collision(atomRockets[i].Center, 23, turrets[j].Center, turrets[j].Radius, worldSize))
                        atomRockets[i].DestroyMe = true;
                }

                for (j = 0; j < alienShips1.Count; j++)
                {
                    if (CollisionHelper.Collision(atomRockets[i].Center, 23, alienShips1[j].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(rocketColor, atomRockets[i].Center, 0, TextureProvider.ColAlien1, alienShips1[j].Center, alienShips1[j].RotationAngle) == true)
                            atomRockets[i].DestroyMe = true;
                    }
                }

                for (j = 0; j < alienShips2.Count; j++)
                {
                    if (CollisionHelper.Collision(atomRockets[i].Center, 23, alienShips2[j].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(rocketColor, atomRockets[i].Center, 0, TextureProvider.ColAlien2, alienShips2[j].Center, alienShips2[j].RotationAngle) == true)
                            atomRockets[i].DestroyMe = true;
                    }
                }

                for (j = 0; j < alienShips3.Count; j++)
                {
                    if (CollisionHelper.Collision(atomRockets[i].Center, 23, alienShips3[j].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(rocketColor, atomRockets[i].Center, 0, TextureProvider.ColAlien3, alienShips3[j].Center, alienShips3[j].RotationAngle) == true)
                            atomRockets[i].DestroyMe = true;
                    }
                }
                for (j = 0; j < alienShips4.Count; j++)
                {
                    if (CollisionHelper.Collision(atomRockets[i].Center, 23, alienShips4[j].Center, 32, worldSize))
                    {
                        if (CollisionHelper.PixelCollision(rocketColor, atomRockets[i].Center, 0, TextureProvider.ColAlien4, alienShips4[j].Center, alienShips4[j].RotationAngle) == true)
                            atomRockets[i].DestroyMe = true;
                    }
                }

                // Player 1
                if (CollisionHelper.Collision(atomRockets[i].Center, 23, ship1.Center, 32, worldSize))
                {
                    if (CollisionHelper.PixelCollision(rocketColor, atomRockets[i].Center, 0, TextureProvider.ColPlayer1, ship1.Center, ship1.RotationAngle) == true)
                        atomRockets[i].DestroyMe = true;
                }

                // Player 2
                if (CollisionHelper.Collision(atomRockets[i].Center, 23, ship2.Center, 32, worldSize))
                {
                    if (CollisionHelper.PixelCollision(rocketColor, atomRockets[i].Center, 0, TextureProvider.ColPlayer2, ship2.Center, ship2.RotationAngle) == true)
                        atomRockets[i].DestroyMe = true;
                }
            }
        }

        /// <summary>
        /// Schadensverteilung nach Explosion von Atomrakete
        /// </summary>
        /// <param name="centerExplosion"></param>
        /// <param name="asteroids"></param>
        /// <param name="mines"></param>
        /// <param name="turrets"></param>
        /// <param name="alienShips1"></param>
        /// <param name="alienShips2"></param>
        /// <param name="alienShips3"></param>
        /// <param name="alienShips4"></param>
        /// <param name="ship1"></param>
        /// <param name="ship2"></param>
        /// <param name="worldSize"></param>
        public static void DistributeDamageOfAtomRocket(Vector2 centerExplosion, List<Asteroid> asteroids, List<Mine> mines, List<Turret> turrets, List<AlienShip1> alienShips1, List<AlienShip2> alienShips2,
            List<AlienShip3> alienShips3, List<AlienShip4> alienShips4, PlayerShip ship1, PlayerShip ship2, int worldSize)
        {
            const int max_distance = 450;   // Explosionsradius
            float distance;
            int j;

            for (j = 0; j < asteroids.Count; j++)
            {
                distance = CollisionHelper.GetDistance(centerExplosion, asteroids[j].Center, worldSize);
                if (distance <= max_distance)
                    asteroids[j].Hit((int)(max_distance - distance));
            }

            for (j = 0; j < mines.Count; j++)
            {
                distance = CollisionHelper.GetDistance(centerExplosion, mines[j].Center, worldSize);
                if (distance <= max_distance)
                    mines[j].Hit((int)(max_distance - distance));
            }

            for (j = 0; j < turrets.Count; j++)
            {
                distance = CollisionHelper.GetDistance(centerExplosion, turrets[j].Center, worldSize);
                if (distance <= max_distance)
                    turrets[j].Hit((int)(max_distance - distance));
            }

            for (j = 0; j < alienShips1.Count; j++)
            {
                distance = CollisionHelper.GetDistance(centerExplosion, alienShips1[j].Center, worldSize);
                if (distance <= max_distance)
                    alienShips1[j].Hit((int)(max_distance - distance));
            }

            for (j = 0; j < alienShips2.Count; j++)
            {
                distance = CollisionHelper.GetDistance(centerExplosion, alienShips2[j].Center, worldSize);
                if (distance <= max_distance)
                    alienShips2[j].Hit((int)(max_distance - distance));
            }

            for (j = 0; j < alienShips3.Count; j++)
            {
                distance = CollisionHelper.GetDistance(centerExplosion, alienShips3[j].Center, worldSize);
                if (distance <= max_distance)
                    alienShips3[j].Hit((int)(max_distance - distance));
            }

            for (j = 0; j < alienShips4.Count; j++)
            {
                distance = CollisionHelper.GetDistance(centerExplosion, alienShips4[j].Center, worldSize);
                if (distance <= max_distance)
                    alienShips4[j].Hit((int)(max_distance - distance));
            }

            // Player 1
            distance = CollisionHelper.GetDistance(centerExplosion, ship1.Center, worldSize);
            if (distance <= max_distance)
                ship1.Hit((int)(max_distance - distance));

            // Player 2
            distance = CollisionHelper.GetDistance(centerExplosion, ship2.Center, worldSize);
            if (distance <= max_distance)
                ship2.Hit((int)(max_distance - distance));
        }

        #endregion Public methods

        #region Private methods

        /// <summary>
        /// Annäherungserkennung für einen Turm
        /// </summary>
        /// <param name="turretCenter"></param>
        /// <param name="proximityDistance"></param>
        /// <returns></returns>
        private static bool ProximityDetectionTurret(PlayerShip ship1, PlayerShip ship2, Vector2 turretCenter, int proximityDistance, ref Vector2 target, int worldSize)
        {
            // Annäherung Schiff Spieler 1
            if (CollisionHelper.Collision(turretCenter, proximityDistance, ship1.Center, 32, worldSize) == true)
            {
                target = ship1.Center;
                return true;
            }

            // Annäherung Schiff Spieler 2
            if (CollisionHelper.Collision(turretCenter, proximityDistance, ship2.Center, 32, worldSize) == true)
            {
                target = ship2.Center;
                return true;
            }

            return false;
        }

        /// <summary>
        /// Colorarray für verschiedene Asteroidengrößen zurückliefern
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        private static Color[,] GetAsteroidColorArray(AsteroidSize size)
        {
            switch (size)
            {
                case AsteroidSize.big:
                    return TextureProvider.ColAsteroidBig;
                case AsteroidSize.medium:
                    return TextureProvider.ColAsteroidMedium;
                case AsteroidSize.small:
                default:
                    return TextureProvider.ColAsteroidSmall;
            }
        }

        #endregion private methods
    }
}
